#include "Dialog.h"
#include "Player.h"
#include "Inventory.h"
#include "DialogManager.h"

const char Dialog::className[] = "Dialog";

#define method(class, name) {#name, &class::name}

Lunar<Dialog>::RegType Dialog::methods[] = {
	method(Dialog, addRequestDialog),
	method(Dialog, addClueDialog),
	method(Dialog, addFinalDialog),
};

Dialog::Dialog(lua_State* L) : m_edialogType(Dialog::NONE), m_currentDialog(0), m_bdialogOver(false) {
}

Dialog::~Dialog() {
}

void Dialog::playDialog(int idClue) {
	if((m_edialogType == Dialog::NONE)&&(!m_bdialogOver)) {
		// Check if player has the clue
		if(StageManager::getInstance().getCurrentStage()->getPlayer()->getInventory()->hasCloth(idClue)) {
			m_edialogType = Dialog::FINAL;
		}
		else if( (StageManager::getInstance().getCurrentStage()->getPlayer()->getInventory()->hasClue(idClue)) ||
				 (StageManager::getInstance().getCurrentStage()->getPlayer()->getInventory()->gaveClue(idClue)) ){
			m_edialogType = Dialog::CLUE;
			StageManager::getInstance().getCurrentStage()->getPlayer()->getInventory()->removeClue(idClue);
			ObjectManager::getInstance().unlockCloth(idClue);
		}
		else {
			m_edialogType = Dialog::REQUEST;
		}
		m_currentDialog = 0;
	}

	if(m_edialogType == Dialog::REQUEST) {
		DialogManager::getInstance().removeText();
		DialogManager::getInstance().displayText(m_vrequestDialogs[m_currentDialog]);

		m_currentDialog++;
		if((unsigned int)m_currentDialog >= m_vrequestDialogs.size()) {
			m_bdialogOver = true;
		}
	}
	else if(m_edialogType == Dialog::CLUE) {
		DialogManager::getInstance().removeText();
		DialogManager::getInstance().displayText(m_vclueDialogs[m_currentDialog]);

		m_currentDialog++;
		if(m_currentDialog >= m_vclueDialogs.size()) {
			m_bdialogOver = true;
		}
	}
	else if(m_edialogType == Dialog::FINAL) {
		DialogManager::getInstance().removeText();
		DialogManager::getInstance().displayText(m_vfinalDialogs[m_currentDialog]);

		m_currentDialog++;
		if(m_currentDialog >= m_vfinalDialogs.size()) {
			m_bdialogOver = true;
		}
	}
}

void Dialog::stopDialog() {
	m_edialogType = Dialog::NONE;
	m_bdialogOver = false;

	DialogManager::getInstance().removeText();
}

int Dialog::addRequestDialog(lua_State* L) {
	// Read dialog
	std::string dialog;

	int nbArgs = lua_gettop(L);
	if (nbArgs == 1 && lua_isstring(L,1)) {
		dialog = lua_tostring(L,1);
	}
	else {
		return 0;
	}
	m_vrequestDialogs.push_back(dialog);

	return 0;
}

int Dialog::addClueDialog(lua_State* L) {
	// Read dialog
	std::string dialog;

	int nbArgs = lua_gettop(L);
	if (nbArgs == 1 && lua_isstring(L,1)) {
		dialog = lua_tostring(L,1);
	}
	else {
		return 0;
	}
	m_vclueDialogs.push_back(dialog);

	return 0;
}

int Dialog::addFinalDialog(lua_State* L) {
	// Read dialog
	std::string dialog;

	int nbArgs = lua_gettop(L);
	if (nbArgs == 1 && lua_isstring(L,1)) {
		dialog = lua_tostring(L,1);
	}
	else {
		return 0;
	}
	m_vfinalDialogs.push_back(dialog);

	return 0;
}